package com.lk.oss.Controller;

import com.lk.oss.utils.AliOssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.UUID;

@RequestMapping("/image")
@Controller
public class uploadController {

    @Autowired
    private AliOssUtil aliOssUtil;

    @RequestMapping("/upload")
    public String upload(MultipartFile file){
        System.out.println("uploadController...");
        try {
            //原始文件名
            String originalFilename = file.getOriginalFilename();
            //截取原始文件名的后缀
            String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
            //构造新文件名
            String objectName = UUID.randomUUID().toString();
            objectName=objectName+extension;
            //文件请求路径
            String Path = aliOssUtil.upload(file.getBytes(), objectName);

            System.out.println("图片路径："+Path);
            return "redirect:/index.jsp";

        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
